// pages/route/special/special.js
const app = getApp();
const service = require('../../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 路线类型
    routeType: 4,
    // 预定时间
    time: null,
    // 选择的联系人
    liaison: {},
    // 乘车人数
    rideNumber: 1,
    // 选择的优惠券
    coupon: null,
    // 最终价格
    finallyPrice: '--',
    // 备注
    memo: '',
    lineData: {
      addressFrom: '',
      addressTo: '',
      areaForm: '',
      areaTo: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      lineId: options.lineId,
      chooseTime: options.chooseTime,
      finallyPrice: options.price
    })
    this.loadData();
  },

  // 加载信息
  loadData() {
    let that = this;
    let data = {
      lineId: this.data.lineId,
      chooseTime: this.data.chooseTime
    }
    service.getConnect(service.api.getFixedRouteDetails, data).then(function(res) {
      wx.hideLoading();
      that.setData(res.data);
    });
  },

  // 时间选择
  bindTimeChange(e) {
    this.setData({
      time: e.detail.value
    });
  },

  // 选择联系人
  bindSelectLiaison() {
    wx.navigateTo({
      url: '/pages/center/liaison/liaison?source=route',
    })
  },

  // 票数减少
  bindTicketCountDown() {
    if (this.data['rideNumber'] > 1) {
      this.setData({
        rideNumber: this.data['rideNumber'] - 1,
        coupon: null,
        // finallyPrice: parseFloat(this.data['lineData']['price']) * (this.data['rideNumber'] - 1)
      })
    }
  },

  // 票数增加
  bindTicketCountUp(e) {
    if (this.data['rideNumber'] < this.data.carData.seat - 1) {
      this.setData({
        rideNumber: this.data['rideNumber'] + 1,
        // finallyPrice: parseFloat(this.data['lineData']['price']) * (this.data['rideNumber'] + 1)
      })
    }
  },

  // 备注输入
  bindMemoInput(e) {
    this.setData({
      memo: e.detail.value
    })
  },

  // 选择优惠券
  toSelectCoupon() {
    let money = parseFloat(this.data.finallyPrice) + (this.data.coupon && parseFloat(this.data.coupon['amount']));
    wx.navigateTo({
      url: '/pages/route/selectCoupon/selectCoupon?money=' + money
        + '&type=' + this.data.routeType,
    })
  },

  // 立刻预定
  submitSpecialRouteOrder() {
    let that = this;
    let data = this.data;
    if (data['time'] === null) {
      app.showConfirm('提示', '请选择出发时间~', false);
      return;
    };
    if (!data['liaison']['id']) {
      app.showConfirm('提示', '请选择联系人', false, function() {
        wx.navigateTo({
          url: '/pages/center/liaison/liaison?source=route',
        })
      });
      return;
    };
    let params = {
      orderKey: data['orderKey'],
      chooseTime: data.chooseTime,
      shiftTime: data['time'],
      contactId: data['liaison']['id'],
      rideNumber: data['rideNumber'],
      memo: data['memo']
    }
    data['coupon'] && (params['coupon'] = data['coupon']['id']);
    data['coupon'] && (params['userCouponId'] = data['coupon']['userCouponId']);
    service.getConnect(service.api.sumbitFixedRouteOrder, params).then(function(res) {
      wx.hideLoading();
      that.doWxPay(res.data)
    });
  },

  // 调起微信支付
  doWxPay(e) {
    let that = this;
    if (e.appId) {
      wx.requestPayment({
        timeStamp: e.timeStamp,
        nonceStr: e.nonceStr,
        package: e.package,
        signType: e.signType,
        paySign: e.paySign,
        success(res) {
          console.log(res);
          wx.showToast({
            title: '支付成功~',
            mask: true
          })
          wx.showLoading({
            title: '请稍后...',
            mask: true
          });
          that.checkPayStatus(e.orderSn);
        },
        fail(res) {
          console.log(res);
          wx.navigateTo({
            url: '/pages/order/details/details?orderSn=' + e.orderSn,
          })
          // app.showConfirm('提示', '支付失败,是否前往订单页', true, function () {
          //   wx.navigateTo({
          //     url: '/pages/order/details/details?orderSn=' + e.orderSn,
          //   })
          // }, function () {
          //   that.loadData();
          // });
        }
      })
    } else {
      wx.showToast({
        title: '支付成功~',
        mask: true
      })
      that.checkPayStatus(e.orderSn);
    }
  },

  checkPayStatus(orderSn) {
    let that = this;
    service.getConnect(service.api.checkOrderPayStatus, {
      orderSn: orderSn
    }).then(function(res) {
      console.log(res);
      wx.hideLoading();
      if (res.data.isPay === 1) {
        // 跳转订单详情
        // wx.redirectTo({
        //   url: '/pages/order/details/details?orderSn=' + orderSn,
        // })
        // 跳转支付结果
        wx.navigateTo({
          url: '/pages/order/payResult/index?orderSn=' + orderSn,
        })
      } else {
        setTimeout(function() {
          that.checkPayStatus(orderSn);
        }, 1000);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})